package com.solcoo.customer.base;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.widget.BaseAdapter;

import com.android.common.util.CollectionUtil;
import com.solcoo.customer.model.BaseListDO;

/**
 * @Description
 * 
 * @author panjunlin
 * 
 * @version 创建时间：2012-9-27 上午10:17:05
 * 
 */
public abstract class BaseListAdapter<T> extends BaseAdapter {

	protected List<T> mdata;

	protected Context context;

	protected boolean hasMore = true;

	public int currentPage = 1;

	public int pageSize = 10;

	public long cursor = 0;

	public BaseListAdapter(Context context) {
		this(context, null);
	}

	public BaseListAdapter(Context context, List<T> mdata) {
		this.context = context;
		this.mdata = mdata == null ? new ArrayList<T>() : mdata;
	}

	@Override
	public int getCount() {
		return mdata.size();
	}

	@Override
	public T getItem(int position) {
		return CollectionUtil.get(mdata, position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	protected void clear() {
		this.mdata.clear();
	}

	public boolean add(BaseListDO<T> result) {
		if (result == null || result.isEmpty()) {
			return false;
		}
		this.mdata.addAll(result.getData());
		cursor = result.youbiao;
		hasMore = cursor > 0;
		notifyDataSetChanged();
		return true;
	}

	public boolean update(BaseListDO<T> result) {
		if (result == null || result.isEmpty()) {
			return false;
		}
		this.mdata = result.getData();
		cursor = result.youbiao;
		hasMore = cursor > 0;
		notifyDataSetChanged();
		return true;
	}

	//
	// protected void setHasMore(ListResult<T> result) {
	// if (result != null) {
	// hasMore = currentPage < result.totalPage;
	// }
	// }

	public boolean hasMore() {
		return hasMore;
	}

	public interface IResultWrapper<T> {
		boolean isEmpty();

		List<T> getData();
	}

}
